const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl: imageUrl,
    MyAddress: [],
    _index: -1,
    id: '',
    type: 2,
    good_id: '',
    isuse: 2,
  },
  onLoad: function (options) {
    wx.removeStorageSync('change_address')
    wx.removeStorageSync('address_id')
    this.setData({
      type: options.type,
      good_id: options.good_id
    })
  },
  onShow() {
    this.getMyAddress()
  },
  toaddAddress() {
    wx.navigateTo({
      url: './addAddress',
    })
  },
  getMyAddress() {
    app.globalData.post({
      data: {
        do: 'MyAddress',
        user_id: wx.getStorageSync('userInfo').id,
      }
    }).then(res => {
      this.setData({
        MyAddress: res.data,
        _index: -1,
        id: '',
      })
    })
  },
  set_default(e) {
    app.globalData.post({
      data: {
        do: 'AddDefault',
        id: e.currentTarget.dataset.id
      }
    }).then(res => {
      this.getMyAddress()
    })
  },
  delete_address(e) {
    let _this = this;
    wx.showModal({
      content: '确定要删除该地址吗？',
      confirmText: '删除',
      confirmColor: '#ee6e01',
      success(res) {
        if (res.confirm) {
          app.globalData.post({
            data: {
              do: 'DelAdd',
              id: e.currentTarget.dataset.id
            }
          }).then(res => {
            _this.getMyAddress()
          })
        }
      }
    })
  },
  edit_address(e) {
    wx.navigateTo({
      url: './addAddress?id=' + e.currentTarget.dataset.id
    })
  },
  enter(e) {
    this.setData({
      _index: e.currentTarget.dataset.index,
      id: e.currentTarget.dataset.id,
      isuse: e.currentTarget.dataset.isuse,
    })
  },
  confirm() {
    if (this.data.id == '') {
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      })
    } else {
      if (this.data.isuse == 1) {
        wx.setStorageSync('id', this.data.id)
        wx.setStorageSync('address_id', this.data.id)
        wx.setStorageSync('change_address', this.data.MyAddress[this.data._index])
        wx.navigateBack()
      } else {
        app.showModal('请重新编辑服务地址', true).then(() => {
          wx.navigateTo({
            url: './addAddress?id=' + this.data.id
          })
        })
      }

    }
  }
})